<style>
.information {
  padding: 20px 40px;
}
</style>
<template>
  <div>
    <div class="information">
      <el-form
        :model="userForm"
        status-icon
        :rules="rules"
        ref="userForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户姓名" prop="user">
          <el-input type="text" v-model="userForm.user" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户账号" prop="user">
          <el-input type="text" v-model="userForm.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" prop="pass">
          <el-input type="password" v-model="userForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户性别" prop="gender">
          <el-input type="text" v-model="userForm.gender" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户年龄" prop="age">
          <el-input type="text" v-model="userForm.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('userForm')">提交</el-button>
          <el-button @click="resetForm('userForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      userForm: {
        userName: "",
        pass: "",
        user: "",
        age: "",
        gender: ""
      },
      rules: {
        user: [{ required: true, message: "请输入用户姓名", trigger: "blur" }],
        pass: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          { min: 6, max: 14, message: "长度在 6 到 16 个字符", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "请输入用户账号", trigger: "blur" }
        ],
        age: [{ required: true, message: "年龄不能为空", trigger: "blur" }],
        gender: [{ required: true, message: "性别不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(userForm) {
      let _this = this;
      this.$refs[userForm].validate(valid => {
        if (valid) {
          this.axios
            .post("/api/addUser", {
              // userName,user,pass,age,gender
              user: _this.userForm.user,
              userName: _this.userForm.userName,
              pass: _this.userForm.pass,
              age: _this.userForm.age,
              gender: _this.userForm.gender
            })
            .then(res => {
              this.$message({
                showClose: true,
                message: "用户添加成功",
                type: "success"
              });
              _this.userForm.user = "";
              _this.userForm.userName = "";
              _this.userForm.pass = "";
              _this.userForm.age = "";
              _this.userForm.gender = "";
              console.log(res);
            });
            // this.$router.push('userList')
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

